package com.songaw.cangku.controller;

import com.songaw.cangku.base.util.UuidUtils;
import com.songaw.cangku.mapper.*;
import com.songaw.cangku.pojo.PageInfo;
import com.songaw.cangku.pojo.Result;
import com.songaw.cangku.pojo.domain.*;
import com.songaw.cangku.pojo.dto.ClothesDto;
import com.songaw.cangku.pojo.entity.*;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author songaw
 * @date 2018/4/1 16:24
 */
@RestController
public class ClothesTableController {
    @Autowired
    ClothesTableMapper clothesTableMapper;
    @Autowired
    ClothesTableExMapper clothesTableExMapper;
    @Autowired
    ClothesExMapper clothesExMapper;

    @Autowired
    ColorsMapper colorsMapper;
    @Autowired
    AppAccountExMapper appAccountExMapper;
    @Autowired
    ClothesTableLogMapper clothesTableLogMapper;
    @ApiOperation(value = "领货", notes = "领货")
    @RequestMapping(path="/v1/clothesTables/linghuo",method = RequestMethod.POST)
    public Result<ClothesTable> createClothesTableAccount(@RequestBody AddClothesTableVo vo){
        try {
            //查询颜色对应的库存
            String modelNumber = vo.getModelNumber();
            if(modelNumber ==null){
                return Result.getSystemErrorResult("款号不能为空");
            }
            if(vo.getWorkerNum() ==null){
                return Result.getSystemErrorResult("工号不能为空");
            }
            if(vo.getClaimed()==null||vo.getClaimed()==0){
                return Result.getSystemErrorResult("领货数量不能为0");
            }
            AppAccountVo appAccount = new AppAccountVo();
            appAccount.setEmplno(vo.getWorkerNum());
            List<AppAccount> appAccounts = appAccountExMapper.select(appAccount);
            if(appAccounts==null||appAccounts.size()<1){
                return Result.getSystemErrorResult("工号不存在");
            }
            ClothesVo searchVo = new ClothesVo();
            searchVo.setModelNumber(modelNumber);
            List<ClothesDto> list = clothesExMapper.select(searchVo);
            if(list==null||list.size()<1){
                return Result.getSystemErrorResult("该款号不存在");
            }
            ClothesDto clothes = list.get(0);
            List<Colors> colorsList = clothes.getColorList();
            Colors color =null;
            for(Colors c :colorsList){
                if(c.getColorName().equals(vo.getColor())&&c.getSizeName().equals(vo.getSize())){
                    color=c;
                }
            }
            if(color==null){
                return Result.getSystemErrorResult("对应颜色或尺码不存在");
            }

            if(color.getColorClaimed().intValue()==color.getColorNum().intValue()){
                return Result.getSystemErrorResult("该颜色没有库存了");
            }
            //领货
            Integer claimed = vo.getClaimed();
            if(claimed.intValue()+color.getColorClaimed().intValue()>color.getColorNum().intValue()){
                return Result.getSystemErrorResult("领货数量超出颜色库存数量");
            }
            if(claimed.intValue()+color.getSizeClaimed().intValue()>color.getSizeNum().intValue()){
                return Result.getSystemErrorResult("领货数量超出尺码库存数量");
            }
            color.setColorClaimed(claimed.intValue()+color.getColorClaimed().intValue());
            color.setSizeClaimed(claimed.intValue()+color.getSizeClaimed().intValue());
            colorsMapper.updateByPrimaryKey(color);
            ClothesTable clothesTable = new ClothesTable();
            BeanUtils.copyProperties(vo,clothesTable);
            clothesTable.setUuid(UuidUtils.base58Uuid());
            clothesTable.setDeleteFlag("0");
            clothesTable.setStatus(1);
            clothesTable.setInsertTime(new Date().getTime());
            clothesTableMapper.insert(clothesTable);
            ClothesTableLog clothesTableLog = new ClothesTableLog();
            BeanUtils.copyProperties(clothesTable,clothesTableLog);
            clothesTableLog.setUuid(UuidUtils.base58Uuid());
            clothesTableLogMapper.insert(clothesTableLog);
        return Result.getSuccessResult(clothesTable);
        }catch (Exception e){
            return Result.getSystemErrorResult(e.getMessage());
        }
    }


    @ApiOperation(value = "根据ID查找", notes = "根据ID查找")
    @RequestMapping(path="/v1/clothesTables/{uuid}",method = RequestMethod.GET)
    public Result<ClothesTable> findByClothesTableUuid(@PathVariable("uuid") String uuid){
        try {

            ClothesTable clothesTable= clothesTableMapper.selectByPrimaryKey(uuid);
            if(clothesTable!=null) {
                return Result.getSuccessResult(clothesTable);
            }else{
                return Result.getSystemErrorResult("没有查到数据");
            }
        }catch (Exception e){
            return Result.getSystemErrorResult(e.getMessage());
        }
    }
    @ApiOperation(value = "交货", notes = "交货")
    @RequestMapping(path="/v1/clothesTables/jiaohuo",method = RequestMethod.PUT)
    public Result<Integer> updateClothesTable(@RequestBody UpdateClothesTableVo vo){
        try {
            ClothesTable clothesTable= clothesTableMapper.selectByPrimaryKey(vo.getUuid());
            if(clothesTable==null) {
                return Result.getSystemErrorResult("没有查到数据");
            }
            //查询颜色对应的库存
            String modelNumber = vo.getModelNumber();
            if(modelNumber ==null){
                return Result.getSystemErrorResult("款号不能为空");
            }
            ClothesVo searchVo = new ClothesVo();
            searchVo.setModelNumber(modelNumber);
            List<ClothesDto> list = clothesExMapper.select(searchVo);
            if(list==null||list.size()<1){
                return Result.getSystemErrorResult("该款号不存在");
            }
            ClothesDto clothes = list.get(0);
            List<Colors> colorsList = clothes.getColorList();
            Colors color =null;
            for(Colors c :colorsList){
                if(c.getColorName().equals(vo.getColor())&&c.getSizeName().equals(vo.getSize())){
                    color=c;
                }
            }
            if(color==null){
                return Result.getSystemErrorResult("对应颜色不存在");
            }

           /* if(color.getColorSubmit().intValue()==color.getColorNum()){
                return Result.getSystemErrorResult("该颜色已经交满了");
            }*/

            //交货
            Integer submit = vo.getSubmit();
            //如果交货大于领货 则修改领货数量为交货数量
           if(submit.intValue()+color.getColorSubmit().intValue()>color.getColorClaimed().intValue()){

               color.setColorClaimed(submit.intValue()+color.getColorSubmit().intValue());

            }
            if(submit.intValue()+color.getSizeSubmit().intValue()>color.getSizeClaimed().intValue()){

                color.setSizeClaimed(submit.intValue()+color.getSizeSubmit().intValue());
            }
            if(vo.getStatus()==-1){
                color.setColorClaimed(submit.intValue()+color.getColorSubmit().intValue());
                color.setSizeClaimed(submit.intValue()+color.getSizeSubmit().intValue());
            }else {
                clothesTable.setStatus(vo.getStatus());
            }

            color.setColorSubmit(submit.intValue()+color.getColorSubmit().intValue());
            color.setSizeSubmit(submit.intValue()+color.getSizeSubmit().intValue());

            colorsMapper.updateByPrimaryKey(color);
           // BeanUtils.copyProperties(vo,clothesTable);
           // vo.setUuid(UuidUtils.base58Uuid());
            clothesTable.setUpdateTime(new Date().getTime());
            Integer rSubmit=clothesTable.getSubmit();
            if(rSubmit==null){
                rSubmit=0;
            }
            clothesTable.setSubmit(submit.intValue()+rSubmit.intValue());
            
            clothesTableMapper.updateByPrimaryKeySelective(clothesTable);
            ClothesTableLog clothesTableLog = new ClothesTableLog();
            BeanUtils.copyProperties(clothesTable,clothesTableLog);
            clothesTableLog.setSubmit(vo.getSubmit());
            clothesTableLog.setUuid(UuidUtils.base58Uuid());
            clothesTableLog.setStatus(2);
            clothesTableLog.setClaimedDate(vo.getSubmitDate());
            clothesTableLogMapper.insert(clothesTableLog);
            return Result.getSuccessResult(1);
        }catch (Exception e){
            return Result.getSystemErrorResult(e.getMessage());
        }
    }
    @RequestMapping(path="/v1/clothesTables",method = RequestMethod.DELETE)
    public Result<Integer> deleteClothesTable(@RequestParam("uuid") String uuid){
        try {
            ClothesTable c =clothesTableMapper.selectByPrimaryKey(uuid);
            if(c!=null){
                c.setDeleteFlag("1");
                clothesTableMapper.updateByPrimaryKey(c);
            }else{
                return Result.getSystemErrorResult("不存在");
            }

            return Result.getSuccessResult(1);
        }catch (Exception e){
            return Result.getSystemErrorResult(e.getMessage());
        }
    }
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(path = "/v1/clothesTables/list/search",method = RequestMethod.GET)
    public Result<PageInfo> findClothesTablesPage(@ModelAttribute ClothesTableVo searchVo){
        PageInfo pageInfo = new PageInfo();
        List<ClothesTable>  list=  clothesTableExMapper.selectLog(searchVo,searchVo.getRowBounds());
        int count = clothesTableExMapper.selectLogCount(searchVo);
        pageInfo.setPageIndex(searchVo.getPageIndex());
        pageInfo.setPageSize(searchVo.getPageSize());
        pageInfo.setTotal(count);
        pageInfo.setResults(list);
        return Result.getSuccessResult(pageInfo);
    }
    @ApiOperation(value = "分页查询记工明细", notes = "分页查询记工明细")
    @RequestMapping(path = "/v1/clothesTableLogs/list/search",method = RequestMethod.GET)
    public Result<PageInfo> findClothesTableLogsPage(@ModelAttribute ClothesTableVo searchVo){
        PageInfo pageInfo = new PageInfo();
        List<ClothesTable>  list=  clothesTableExMapper.selectLog(searchVo,searchVo.getRowBounds());
        int count = clothesTableExMapper.selectLogCount(searchVo);
        pageInfo.setPageIndex(searchVo.getPageIndex());
        pageInfo.setPageSize(searchVo.getPageSize());
        pageInfo.setTotal(count);
        pageInfo.setResults(list);
        return Result.getSuccessResult(pageInfo);
    }
    @ApiOperation(value = "根据员工工号统计", notes = "根据员工工号统计")
    @RequestMapping(path = "/v1/clothesTables/list/searchByWorkerNum",method = RequestMethod.GET)
    public Result<List<Map<String,Object>>>findClothesTableByWorkerNum(@RequestParam("workerNum") String workerNum,@RequestParam(value = "beginSubmitDate",required = false) String beginSubmitDate,@RequestParam(value = "endSubmitDate",required = false) String endSubmitDate ){

        List<Map<String,Object>> list =  clothesTableExMapper.selectByWorkerNum(workerNum,beginSubmitDate,endSubmitDate);

        return Result.getSuccessResult(list);
    }
    @ApiOperation(value = "不分页查询", notes = "不分页查询")
    @RequestMapping(path = "/v1/clothesTables/list",method = RequestMethod.GET)
    public Result<List<ClothesTable>> findClothesTableAccounts(@ModelAttribute  ClothesTableVo searchVo){
        return Result.getSuccessResult(clothesTableExMapper.select(searchVo));

    }
}
